home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-19 | 8.2 KB | 305 lines | [TEXT/CWIE] |
- // Program Author: Paul Baxter
- // pbaxter@assistivetech.com
- //
- //
-
- #include <Retrace.h>
- #include <DeskBus.h>
- #include <Speech.h>
- #include <Ctype.h>
- #include <String.h>
-
- #include "pref.h"
- #include "globals.h"
- #include "speech.h"
-
- static Boolean isEndofWord(char ch);
- static Boolean isEndofSentence(char ch);
-
- // * ****************************************************************************** *
- // * AppendCharBuffer
- // * Add a KeyCode to the char buffer
- // * This gets called during a keyboard interupt after A5 is set.
- // * gPrefs and gPronounce MUST be locked!!
- // * ****************************************************************************** *
- void AppendCharBuffer(char theKey)
- {
- short index, pindex;
- StringPtr theString;
-
- if ((**gPrefs).speakChars) {
- if (gCharIndex >= kCharBuffSize -1) {
- BlockMoveData(&gCharBuffer[1], gCharBuffer, kCharBuffSize -1);
- gCharIndex = kCharBuffSize -1;
- }
- gCharBuffer[gCharIndex] = ' ';
- gCharIndex++;
- pindex = gPronounceIndex[theKey];
- if (pindex != -1) {
- if (gShiftState)
- theString = gPronounce[pindex].shiftpronounciation;
- else
- theString = gPronounce[pindex].pronounciation;
-
- for (index = 1; index <= theString[0]; index++) {
- if (gCharIndex >= kCharBuffSize -1) {
- BlockMoveData(&gCharBuffer[1], gCharBuffer, kCharBuffSize -1);
- gCharIndex = kCharBuffSize -1;
- }
- gCharBuffer[gCharIndex] = theString[index];
- gCharIndex++;
- }
- return;
- }
- // we don't know what this is??
- gCharIndex = 0;
- }
- }
-
- // * ****************************************************************************** *
- // * AppendWordBuffer
- // * Add a char to the word buffer
- // * ****************************************************************************** *
- void AppendWordBuffer(char theChar)
- {
-
- if (gWordIndex > 0 && isEndofWord(gWordBuffer[gWordIndex -1]))
- return;
-
- if (!isEndofWord(theChar) && !isprint(theChar) && theChar != kDelete)
- return;
-
- if ((**gPrefs).speakWords) {
- // We could do something with arrow keys as well
- if (theChar == kDelete) {
- if (gWordIndex > 0)
- gWordIndex--;
- return;
- }
- if (gWordIndex >= kWordBuffSize -1) {
- BlockMoveData(&gWordBuffer[1], gWordBuffer, kWordBuffSize -1);
- gWordIndex = kWordBuffSize -1;
- }
- gWordBuffer[gWordIndex] = theChar;
- gWordIndex++;
- }
- }
-
- // * ****************************************************************************** *
- // * AppendSentenceBuffer
- // * Add a char to the sentence buffer
- // * ****************************************************************************** *
- void AppendSentenceBuffer(char theChar)
- {
- if (gSentenceIndex > 0 && isEndofSentence(gSentenceBuffer[gSentenceIndex -1]))
- return;
-
- if (!isEndofSentence(theChar) && !isprint(theChar) && theChar != kDelete)
- return;
-
- if ((**gPrefs).speakSentence) {
- if (theChar == kDelete) {
- if (gSentenceIndex > 0)
- gSentenceIndex--;
- return;
- }
- if (gSentenceIndex >= kSentenceBuffSize -1) {
- BlockMoveData(&gSentenceBuffer[1], gSentenceBuffer, kSentenceBuffSize -1);
- gSentenceIndex = kSentenceBuffSize -1;
- }
- gSentenceBuffer[gSentenceIndex] = theChar;
- gSentenceIndex++;
- }
- }
-
- // * ****************************************************************************** *
- // * SpeakChars
- // * speak chars in the char buffer
- // * ****************************************************************************** *
- void SpeakChars(void)
- {
- if (((**gPrefs).speakChars) && (gCharIndex) && (gSpeechChannel)) {
- // give chars precedence over all others
- gLastSpeechChar = true;
- SpeakText(gSpeechChannel, gCharBuffer, gCharIndex);
- gCharIndex = 0;
- }
- }
-
- // * ****************************************************************************** *
- // * SpeakWords
- // * speak any words in the word buffer
- // * ****************************************************************************** *
- void SpeakWords(void)
- {
- short wordindex;
-
- if ((**gPrefs).speakChars && gWordIndex == 1)
- gWordIndex = 0; // avoid garble
-
- if (((**gPrefs).speakWords) && (gWordIndex) && (gSpeechChannel)) {
- wordindex = gWordIndex;
- gWordIndex = gCharIndex = 0;
- gLastSpeechChar = false;
- SpeakText(gSpeechChannel, gWordBuffer, wordindex);
- }
- }
-
- // * ****************************************************************************** *
- // * SpeakSentences
- // * speak any sentences in the sentence buffer
- // * ****************************************************************************** *
- void SpeakSentences(void)
- {
- if ((gSentenceIndex == 1) && ((**gPrefs).speakChars || (**gPrefs).speakWords))
- gSentenceIndex = 0; // avoid garble
-
- if (((**gPrefs).speakSentence) && (gSentenceIndex) && (gSpeechChannel)) {
- gLastSpeechChar = false;
- SpeakText(gSpeechChannel, gSentenceBuffer, gSentenceIndex);
- gWordIndex = 0; // avoid garble
- gCharIndex = 0; // avoid garble
- gSentenceIndex = 0;
- }
- }
-
- // * ****************************************************************************** *
- // * isEndofSentence
- // * is the char an end of sentence marker
- // * ****************************************************************************** *
- static Boolean isEndofSentence(char ch)
- {
- switch (ch) {
- case '.':
- case '?':
- case '!':
- case ';':
- case 0x03: // enter
- case 0x0D: // return
- case 0x1C: // left arrow
- case 0x1D: // right arrow
- case 0x1E: // up arrow
- case 0x1F: // down arrow
- return true;
-
- default:
- return false;
- }
- }
-
- // * ****************************************************************************** *
- // * isEndofWord
- // * is the char an end of word marker
- // * ****************************************************************************** *
- static Boolean isEndofWord(char ch)
- {
- switch (ch) {
- case ' ':
- case '.':
- case '?':
- case '!':
- case ';':
- case ',':
- case '@':
- case '#':
- case '$':
- case '%':
- case '^':
- case '&':
- case '*':
- case '(':
- case ')':
- case '-':
- case '+':
- case '=':
- case '[':
- case ']':
- case '{':
- case '}':
- case ':':
- case '\"':
- case 0x03: // enter
- case 0x0D: // return
- case 0x1C: // left arrow
- case 0x1D: // right arrow
- case 0x1E: // up arrow
- case 0x1F: // down arrow
- return true;
- default:
- return false;
- }
- }
-
- // * ****************************************************************************** *
- // * SpeakBuffers
- // * Speak Appropriate buffer
- // * ****************************************************************************** *
- void SpeakBuffers(void)
- {
- if (((**gPrefs).speakSentence) && (gSentenceIndex > 0) &&
- (isEndofSentence(gSentenceBuffer[gSentenceIndex -1])))
- SpeakSentences();
-
- if (((**gPrefs).speakWords) && (gWordIndex > 0) &&
- (isEndofWord(gWordBuffer[gWordIndex -1])))
- SpeakWords();
-
- SpeakChars();
- }
-
-
- // * ****************************************************************************** *
- // * InitSpeechStrings
- // * init speech strings
- // * ****************************************************************************** *
- void InitSpeechStrings(void)
- {
- Handle ascihandle;
- short count;
- // Get names for Plaintalk
- ascihandle = GetResource('ASCI', 128);
- if (ascihandle) {
- HLockHi(ascihandle);
- DetachResource(ascihandle);
- gPronounce = (KeyPronunciation*) (*ascihandle);
- gNumPronounces = GetHandleSize(ascihandle) / sizeof(KeyPronunciation);
-
- // build indexes into speach
- memset(gPronounceIndex, -1, kNumKeys);
- for (count = 0; count < gNumPronounces; count++)
- gPronounceIndex[gPronounce[count].keyCode] = count;
- }
- }
-
- // * ****************************************************************************** *
- // * InitBuffers
- // * init speech buffers
- // * ****************************************************************************** *
- Boolean InitBuffers(void)
- {
- // allocate our buffers
- gCharBuffer = NewPtrSys(kCharBuffSize);
- gWordBuffer = NewPtrSys(kWordBuffSize);
- gSentenceBuffer = NewPtrSys(kSentenceBuffSize);
- return (gCharBuffer && gWordBuffer && gSentenceBuffer);
- }
-
- // * ****************************************************************************** *
- // * KillBuffers
- // * kill speech buffers
- // * ****************************************************************************** *
- void KillBuffers(void)
- {
- if (gCharBuffer)
- DisposePtr(gCharBuffer);
- if (gWordBuffer)
- DisposePtr(gWordBuffer);
- if (gSentenceBuffer)
- DisposePtr(gSentenceBuffer);
- if (gPronounce)
- DisposeHandle(RecoverHandle((Ptr)gPronounce));
-
- gCharBuffer = gWordBuffer = gSentenceBuffer = (Ptr)gPronounce = nil;
- }
-
-